package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.ForgetBean;
import com.lc.luzhengtang.bean.VerificationCodeBean;
import com.lc.luzhengtang.conn.ConnChangePwd;
import com.lc.luzhengtang.conn.ConnVerificationCode;
import com.lc.luzhengtang.util.LovelyToast;
import com.lc.luzhengtang.view.ClearEditText;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/5.
 */
public class ForgetPasswordActivity2 extends BaseActivity {

    @BindView(R.id.img_news)
    ImageView imgNews;

    @BindView(R.id.edit_phone)
    ClearEditText editPhone;

    @BindView(R.id.edit_code)
    ClearEditText editCode;
    @BindView(R.id.text_sendbtn)
    TextView textSendbtn;


    @BindView(R.id.btn_settings)
    Button btnSettings;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.text_forget_password)
    TextView textForgetPassword;

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                textSendbtn.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                textSendbtn.setText("重新获取");
                textSendbtn.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd2);
        ButterKnife.bind(this);
        textTitle.setText("忘记密码");
    }

    public void getChangePwd() {
        new ConnChangePwd(editPhone.getText().toString().trim(), editPhone.getText().toString().trim(), new AsyCallBack<ForgetBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, ForgetBean forgetBean) throws Exception {
                super.onSuccess(toast, type, o, forgetBean);
                LovelyToast.makeText(ForgetPasswordActivity2.this, forgetBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                finish();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            /*    LovelyToast.makeText(AppealActivity.this, ConnChangePwd.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
             /*   LovelyToast.makeText(AppealActivity.this, ConnChangePwd.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/
            }
        }

        ).execute(ForgetPasswordActivity2.this, false);
    }

    String myCode;

    @OnClick({R.id.img_news, R.id.edit_phone, R.id.edit_code, R.id.text_sendbtn, R.id.btn_settings, R.id.text_forget_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;

            case R.id.edit_phone:
                break;

            case R.id.edit_code:
                break;
            case R.id.text_sendbtn:
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(ForgetPasswordActivity2.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(ForgetPasswordActivity2.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity2.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    textSendbtn.setEnabled(false);
                    getVerificationCode();
                }

                break;


            case R.id.btn_settings:
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(ForgetPasswordActivity2.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity2.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else if (TextUtils.isEmpty(editCode.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity2.this, "验证码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!editCode.getText().toString().equals(myCode)) {

                    LovelyToast.makeText(ForgetPasswordActivity2.this, "验证码不正确", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("mobil", editPhone.getText().toString().toString());

                    Log.d(editPhone.getText().toString().toString(), "!!!!!!!!!!!!!!!!!!!!!!!!!");


                    intent.setClass(ForgetPasswordActivity2.this, Forget2NextActivity.class);
                    startActivity(intent);
                    finish();
                }

                break;

            case R.id.text_forget_password:
                //忘记支付密码
                startActivity(new Intent(ForgetPasswordActivity2.this, AppealActivity.class));

                break;


        }
    }

    public void getVerificationCode() {
        new ConnVerificationCode(editPhone.getText().toString().trim(), "tong", new AsyCallBack<VerificationCodeBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, VerificationCodeBean registerBean) throws Exception {

                LovelyToast.makeText(ForgetPasswordActivity2.this, registerBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
//                btRegistered.setSelected(true);
                myCode = registerBean.getData().getCode() + "";
                countTime.start();
                super.onSuccess(toast, type, o, registerBean);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
                textSendbtn.setEnabled(true);

                textSendbtn.setText("获取验证码");
                countTime.start();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
                countTime.start();
            }
        }


        ).execute(this);
    }


}
